package com.zhangguo.springmvc01.controller;

import com.zhangguo.springmvc01.entities.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author wangxing
 * @version 2020/5/29 20:39 Administrator
 */
/**控制器*/
@Controller
@RequestMapping("/")
public class HomeController {

    private String url="temp";
    /**动作 action*/
    @RequestMapping("")
    public String index(Model model){
        model.addAttribute("msg","welcome my website");
        return "hi";
    }
    /**动作 action*/
    @RequestMapping("/hi")
    public String hi(Model model){
        model.addAttribute("msg","hello spring mvc");
        /**访问的jsp文件名*/
        return "temp";
    }
    private String action_default(Model model,String name,String msg){
        model.addAttribute("name",name);
        model.addAttribute("msg",msg);
        return url;
    }

    //http://localhost:8089/action0?id=1&name=wangxing
    @RequestMapping("/action0")
    public String action0(Model model,int id,String name){
        model.addAttribute("msg",
                "基本类型数据自动映射,{id:"+id+",name:"+name+"}");
        model.addAttribute("name",name);
        return url;
    }
    //http://localhost:8089/action1?id=1&name=wangxing&price=99
    @RequestMapping("/action1")
    public String action1(Model model, Product product){
        model.addAttribute("msg",product);
        model.addAttribute("name","自定义数据类型,自动映射 "+product.getName());
        return url;
    }
    //http://localhost:8089/action2?username=wangxing&product.name=phone
    @RequestMapping("/action2")
    public String action2(Model model, User user){
        model.addAttribute("name","复杂数据类型,自动映射");
        model.addAttribute("msg","{username :"+user.getUsername()+",productname:"+user.getProduct().getName()+"}");
        return url;
    }
    //http://localhost:8089/action3?id=1&id=2&id=3&id=122
    @RequestMapping("/action3")
    public String action3(Model model,int[]id){
        model.addAttribute("name","数组数据,自动映射,使用数组接收返回");
        model.addAttribute("msg", Arrays.toString(id));
        return url;
    }
    //http://localhost:8089/action3_2?id=123&name=bmw&price=100000&ids=1&ids=2&ids=3&ids=14
    @RequestMapping("/action3_2")
    public String action3_2(Model model, Car car){
        model.addAttribute("name","数组数据,自动映射,使用对象接收返回");
        model.addAttribute("msg",car);
        return url;
    }
    //http://localhost:8089/action3_3?items[0].name=phone&items[1].name=car&items[2].name=computer
    @RequestMapping("/action3_3")
    public String action3_3(Model model, ProductList productList){
        model.addAttribute("name","集合类型,自动映射");
        model.addAttribute("msg",productList);
        return url;
    }
    //http://localhost:8089/action4?items[a1].name=phone&items[a1].price=111&items[a2].name=car&items[a3].name=computer
    @RequestMapping("/action4")
    public  String action4(Model model, ProductMap productMap){
        model.addAttribute("name","键值对映射类型,自动映射");
        model.addAttribute("msg",productMap);
        return url;
    }
    //http://localhost:8089/action5
    //http://localhost:8089/action5?id=1
    @RequestMapping("/action5")
    public String action5(Model model, @RequestParam(required = false,defaultValue = "100") int id){
        model.addAttribute("name","@RequestParam 设置参数非必填,默认值为100");
        model.addAttribute("msg",id);
        return url;
    }
    //http://localhost:8089/action6?u=wangxing&u=banxiangli&u=wangyouran&u=王星&u=班向丽&u=王悠然
    @RequestMapping("/action6")
    public  String action6(Model model,@RequestParam(name = "u") List<String> users){
        model.addAttribute("name","使用@RequestParam实现参数别名映射,绑定数组和List");
        model.addAttribute("msg",users);
        return url;
    }
    //http://localhost:8089/form/formData2.jsp
    @RequestMapping("/action7")
    public String action7(Model model,
                          @RequestParam(name = "id") List<Integer> ids,
                          @RequestParam(name="name") String name
    ){
        return action_default(model,"表单提交,选项数据获取使用@RequestParam进行绑定名称数据获取",
                "兴趣爱好id:"+ids.toString()+";姓名:"+name
        );
    }
    //http://localhost:8089/static/login.jsp
    @RequestMapping("/login")
    public void login(@RequestBody  String userName,@RequestBody String pwd){
        System.out.println(userName+" ："+pwd);
    }
    @RequestMapping("/action8")
    public void action8(@RequestBody List<Product> products, HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");
        System.out.println(Arrays.deepToString(products.toArray()));
        try {
            response.getWriter().write("添加成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/action9")
    public void action9(@RequestBody List<Product> products, HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");
        String str =Arrays.deepToString(products.toArray());
        System.out.println(str);
        try {
            response.getWriter().write(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //重定向
    @RequestMapping("/action10")
    public  String action10(Model model){
        return url;
    }
    @RequestMapping("/action11")
    public  String action11(Model model){
        model.addAttribute("asd","asdqwe");
        return "redirect:action10";
    }
    //重定向
    @RequestMapping("/action12")
    public  String action12(Model model,Product product){
        model.addAttribute("message", product);
        System.out.println(model.containsAttribute("product")); // true
        model.addAttribute("msg",product);
        return url;
    }
    @RequestMapping("/action13")
    public  String action13(Model model, RedirectAttributes redirectAttributes){
        Product product = new Product(2, "iPhone7 Plus", 6989.5);
        redirectAttributes.addFlashAttribute("product", product);
        return "redirect:action12";
    }
    @RequestMapping("/action14")
    public String action14(Model model,
                           @ModelAttribute(name="product",binding = true)Product e
    ){
        model.addAttribute("msg",model.containsAttribute("product")+
                "<br/>"+e
                );
        return url;
    }







}
